/**
 * 死锁：
 *  线程持有A锁，试图拿到B锁；另个线程持有B锁，试图拿到A锁，然后互相等待，出现死锁
 *      使用jps -l（查询java进程号）、jstack查看 （打印异常信息）
 */

public class DealThreadDemo {

    public static String lockA = "A";
    public static String lockB = "B";


    public static void main(String[] args) {



        new Thread(new Deal(lockA,lockB)).start();
        new Thread(new Deal(lockB,lockA)).start();

    }


}
class Deal implements Runnable{

    private Object lockA;
    private Object lockB;

    public Deal(Object lockA, Object lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+"获取锁"+lockA);
            /*try {
                Thread.sleep(2L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName()+"获取锁"+lockB);
            }
        }
    }
}